﻿Imports DTO
Imports BUS
Imports System.Text.RegularExpressions

Public Class TiepNhanHS

    Private Sub TiepNhanHS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim bangHS As New DataTable
        Dim HocSinh As New HocSinhBUS

        bangHS = HocSinh.DanhSachHS()
        Me.gridDSHS.DataSource = bangHS

    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Try
            If (Me.txtTenHS.Text.Trim = String.Empty) Then
                MsgBox("Tên HS Không Được bỏ trống", MsgBoxStyle.Exclamation, "warning")
                Exit Sub
            End If

            If (Regex.IsMatch(txtTenHS.Text.Trim, "^[0-9]+$")) Then
                MsgBox("Tên HS Không Được là số", MsgBoxStyle.Exclamation, "warning")
                Exit Sub
            End If
            If (Me.txtDiaChi.Text.Trim = String.Empty) Then
                MsgBox("Địa Chỉ HS Không Được bỏ trống", MsgBoxStyle.Exclamation, "warning")
                Exit Sub
            End If

            If (Me.txtEmail.Text.Trim = String.Empty) Then
                MsgBox("Email HS Không Được bỏ trống", MsgBoxStyle.Exclamation, "warning")
                Exit Sub
            End If


            Dim hs As New HocSinhDTO

            hs.HoTen = Me.txtTenHS.Text
            hs.DiaChi = Me.txtDiaChi.Text
            hs.Email = Me.txtEmail.Text
            hs.MaLH = 11

            Dim s As String()
            s = Split(Me.txtNgaySinh.Text, "/")

            Dim Ngay As Integer
            Dim Thang As Integer
            Dim Nam As Integer

            Ngay = Integer.Parse(s(0))
            Thang = Integer.Parse(s(1))
            Nam = Integer.Parse(s(2))

            Dim Ngay_sinh As New Date(Nam, Thang, Ngay)

            hs.NgaySinh = Ngay_sinh

            If (Me.rdNam.Checked = True) Then
                hs.GioiTinh = "Nam"
            Else
                hs.GioiTinh = "Nữ"
            End If

            Dim hsBus As New HocSinhBUS
            hsBus.Them(hs)

            Me.gridDSHS.DataSource = hsBus.DanhSachHS()

            MsgBox("Thêm Học Sinh Mới Thành Công", MsgBoxStyle.OkOnly)
        Catch ex As Exception
            MsgBox("Thêm Học Sinh Không Thành Công", MsgBoxStyle.Critical)
        End Try
       
    End Sub

    Private Sub btnLamLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamLai.Click
        Try
            Me.txtTenHS.Text = ""
            Me.txtEmail.Text = ""
            Me.txtNgaySinh.Text = ""
            Me.txtDiaChi.Text = ""
            Me.rdNam.Checked = False
            Me.rdNu.Checked = False
        Catch ex As Exception

        End Try
       
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Try
            Dim hs As New HocSinhDTO
            hs.MaHS = Me.gridDSHS.CurrentRow.Cells(0).Value

            Dim hsBus As New HocSinhBUS
            hsBus.Xoa(hs)

            Me.gridDSHS.DataSource = hsBus.DanhSachHS()

            MsgBox("Xóa Học Sinh Thành Công", MsgBoxStyle.OkOnly)
        Catch ex As Exception
            MsgBox("Xóa Học Sinh Không Thành Công", MsgBoxStyle.Critical)
        End Try
        
    End Sub

    Private Sub gridDSHS_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gridDSHS.SelectionChanged
        Try
            If (Me.gridDSHS.SelectedRows.Count > 0) Then
                Me.txtTenHS.Text = Me.gridDSHS.SelectedRows(0).Cells(1).Value
                Me.txtNgaySinh.Text = Me.gridDSHS.SelectedRows(0).Cells(2).Value
                Me.txtDiaChi.Text = Me.gridDSHS.SelectedRows(0).Cells(3).Value
                Me.txtEmail.Text = Me.gridDSHS.SelectedRows(0).Cells(4).Value
                Dim gioitinh As String
                gioitinh = Me.gridDSHS.SelectedRows(0).Cells(5).Value
                If (gioitinh = "Nam") Then
                    Me.rdNam.Checked = True
                Else
                    Me.rdNu.Checked = True
                End If

            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Try
            If (Me.txtTenHS.Text.Trim = String.Empty) Then
                MsgBox("Tên HS Không Được bỏ trống", MsgBoxStyle.Exclamation, "warning")
                Exit Sub
            End If

            If (Regex.IsMatch(txtTenHS.Text.Trim, "^[0-9]+$")) Then
                MsgBox("Tên HS Không Được là số", MsgBoxStyle.Exclamation, "warning")
                Exit Sub
            End If
            If (Me.txtDiaChi.Text.Trim = String.Empty) Then
                MsgBox("Địa Chỉ HS Không Được bỏ trống", MsgBoxStyle.Exclamation, "warning")
                Exit Sub
            End If

            If (Me.txtEmail.Text.Trim = String.Empty) Then
                MsgBox("Email HS Không Được bỏ trống", MsgBoxStyle.Exclamation, "warning")
                Exit Sub
            End If
            Dim hs As New HocSinhDTO
            Dim lhBus As New LopHocBUS

            hs.MaHS = Me.gridDSHS.CurrentRow.Cells(0).Value

            Dim tenLH As String
            tenLH = Me.gridDSHS.CurrentRow.Cells(6).Value

            hs.MaLH = lhBus.LayMaLH(tenLH)

            hs.HoTen = Me.txtTenHS.Text
            hs.NgaySinh = Me.txtNgaySinh.Text
            hs.Email = Me.txtEmail.Text
            hs.DiaChi = Me.txtDiaChi.Text

            If (Me.rdNam.Checked = True) Then
                hs.GioiTinh = "Nam"
            Else
                hs.GioiTinh = "Nữ"
            End If

            Dim hsBus As New HocSinhBUS
            hsBus.Sua(hs)

            Me.gridDSHS.DataSource = hsBus.DanhSachHS()

            MsgBox("Sửa Học Sinh Thành Công", MsgBoxStyle.OkOnly)
        Catch ex As Exception

            MsgBox("Sửa Học Sinh Không Thành Công", MsgBoxStyle.Critical)
        End Try
    End Sub
End Class
